<?php 
global $gallery;

require_once('check_init.php'); 

// Include rest of init that is required to load up albums

if ((is_object($GLOBALS['xoopsUser']) && (get_class($GLOBALS['xoopsUser']) == "xoopsuser")) || isset($_SESSION['gallery_user'])) {
	if (isset($_SESSION['gallery_user'])) 
		$gallery->session->username = $_SESSION['gallery_user'];
	else
		$gallery->session->username = $GLOBALS['xoopsUser']->getVar('uname'); 

	$gallery->user = $gallery->userDB->getUserByUsername($gallery->session->username);
}

echo "<h1>"._XG_TABLECHECK01."</h1>\n";
echo "<p>"._XG_TABLECHECK02._XG_TABLECHECK03._XG_TABLECHECK04."</p>\n";

$FixErrors = (isset($_POST["FixErrors"]));
$FoundErrors = false;
$albumDB = new AlbumDB(FALSE);
$albumList = Array();
$gallery->session->albumName = "";
$numAlbums = $albumDB->numAlbums($gallery->user);
$numPhotos = $albumDB->getCachedNumPhotos($gallery->user);

// Grab all image records from XOOPS database
$image_handler =& xoops_getmodulehandler('image');
$album_images =& $image_handler->getObjects(null, true);

// Function to recursively get all children album names
function getChildren($albumName) {
	global $gallery;
	$myAlbumList = Array();
	$myAlbum = new Album();
	$myAlbum->load($albumName);
	$numPhotos = $myAlbum->numPhotos(1);
	for ($i=1; $i <= $numPhotos; $i++) {
		if ($myAlbum->isAlbumName($i)) {
			$myName = $myAlbum->isAlbumName($i);
			$myAlbumList = array_merge($myAlbumList, Array($myName), getChildren($myName));
		}
	}
	return $myAlbumList;
}

// Pre-scan to build full list of albums
for ($i = 1; $i <= $numAlbums; $i++) {

	$gallery->album = $albumDB->getAlbum($gallery->user, $i);
	$isRoot = $gallery->album->isRoot(); // Only display album if it is a root album
	if ($isRoot) {
		$tmpAlbumName = $gallery->album->fields["name"];
		$albumList = array_merge($albumList, $tmpAlbumName, getChildren($tmpAlbumName));
	}
}

$numAlbums = count($albumList);
echo _XG_TABLECHECK05.": $numAlbums<br />\n";

while (list($i, $chkAlbumName) = each ($albumList)) {

	$chkAlbum = new Album();
	$chkAlbum->load($chkAlbumName);
	$chkAlbumURL = makeAlbumUrl($chkAlbumName);
	$chkAlbumDir = $chkAlbum->getAlbumDir();
	$chkAlbumFoundErrors = false;
	$FixCompacted = false;
	$numPhotos = $chkAlbum->numPhotos(1);
	echo "<br /><br />"._XG_TABLECHECK06." <a href=\"$chkAlbumURL\">$chkAlbumName</a> ($numPhotos "._MD_GALLERY_ITEMS.")<br />";

	for ($j = 1; $j <= $numPhotos; $j++) {
		$chkPhotoID = $chkAlbum->getPhotoId($j);
		$chkPhoto =& $chkAlbum->getPhoto($j); // Reference so you don't just get a copy of the info
		if (!$chkPhoto->getIsAlbumName()) {
			$infoItem = "<br /> &nbsp;"._XG_TABLECHECK06.": ".$chkPhoto->getCaption()."<br />";

			if (!$chkPhoto->isMovie()) {
    			// Check this photo for proper records in the image table
				$xoops_imageid = $chkPhoto->image->getXoopsImageId();
				if (!isset($album_images[$xoops_imageid])) {
					$info = " &nbsp;&nbsp;&nbsp;"._XG_TABLECHECK07." ";
					$isOK = _XG_TABLECHECK08." ($xoops_imageid) ";
					$FoundErrors = true;
					$chkAlbumFoundErrors = true;
					if ($FixErrors) {
						$isOK .= " "._XG_TABLECHECK09."... ";
						$xoops_imageid = $chkPhoto->image->setXoopsImageData($chkAlbumDir);
						$isOK .= _XG_TABLECHECK10.": $xoops_imageid ";
					}
					echo $infoItem . $info . " $isOK<br />\n";
					$infoItem = "";
				}
			}		

			// Check Thumbnail too
			$xoops_imageid = $chkPhoto->thumbnail->getXoopsImageId();
			if (!isset($album_images[$xoops_imageid])) {
				$info = " &nbsp;&nbsp;&nbsp;"._XG_TABLECHECK11." ";
				$isOK = _XG_TABLECHECK08." ($xoops_imageid) ";
				$FoundErrors = true;
				$chkAlbumFoundErrors = true;
				if ($FixErrors) {
					$isOK .= " "._XG_TABLECHECK09."... ";
					$xoops_imageid = $chkPhoto->thumbnail->setXoopsImageData($chkAlbumDir);
					$isOK .= _XG_TABLECHECK10.": $xoops_imageid ";
				}
				echo $infoItem . $info . " $isOK<br />\n";
			}

			// Check for data file bloat
			if (isset($chkPhoto->image->image_handler) || isset($chkPhoto->thumbnail->image_handler) || isset($chkPhoto->highlightImage->image_handler)) {
				$FoundErrors = true;
				$FixCompacted = true;
				if ($FixErrors) {
					unset($chkPhoto->image->image_handler);
					unset($chkPhoto->thumbnail->image_handler);
					unset($chkPhoto->highlightImage->image_handler);
					$chkAlbumFoundErrors = true;
				} 
			}

		}
	}
	if ($FixCompacted && $FixErrors) {
	    echo "<br /> &nbsp;"._XG_TABLECHECK12."<br />";
	} elseif ($FixCompacted) {
	    echo "<br /> &nbsp;"._XG_TABLECHECK13."<br />";
	}
	if ($chkAlbumFoundErrors && $FixErrors) {
		$saveOK = $chkAlbum->save();
		echo "<br /> &nbsp;"._XG_TABLECHECK14.": ";
		if ($saveOK = true) {
			echo _MD_GALLERY_ADMIN_TABLECHECK_OK;
		} else {
			echo _MD_GALLERY_ADMIN_TABLECHECK_ERROR;
		}
		echo "<br /><br />";
	}	
}

If ($FoundErrors && !$FixErrors) {
	echo '<h3>'._XG_TABLECHECK15.'</h3>
		<form action="index.php" method="POST">
		<input type="hidden" name="op" value="check_missing">
		<input type="submit" name="FixErrors" value="'._XG_TABLECHECK16.'">
		</form>';
} elseif ($FoundErrors) {
	echo '<h3>'._XG_TABLECHECK17.'</h3>';
} else {
	echo '<h3>'._XG_TABLECHECK18.'</h3>';
}	
?>